package com.xbb.controller;


import com.xbb.entity.Role;
import com.xbb.entity.Userroles;
import com.xbb.service.IUserrolesService;
import com.xbb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr xie
 * @since 2020-05-19
 */
@RestController
@RequestMapping("/userroles")
public class UserrolesController {

    @Autowired
    IUserrolesService iUserrolesService;

    /**
     *显示指定用户的所有角色---uid
     * @param uid 指定用户
     * @returnid
     */
    @PostMapping("/getRolesByUserId")
    public Object getRolesByUserId(@RequestParam Integer uid){
        List<Role> list =iUserrolesService.getRolesByUserId(uid);
        return list;
    }

    /**
     *给指定用户批量添加角色
     * @param uid 指定的用户 1001
     * @param rids 角色id组成的字符串 1,2,3
     * @return
     */
    @PostMapping("/addRolesByUserId")
    public Object addRolesByUserId(@RequestParam Integer uid,@RequestParam String rids){
       //声明一个集合
        List<Userroles> list =new ArrayList<Userroles>();
        String[] arr = rids.split(",");
        for(String s:arr){
            Integer rid = Integer.valueOf(s);
            Userroles userroles = new Userroles(0,uid,rid);
            list.add(userroles);
        }
        //调用方法，批量添加集合中的数据
        boolean res = iUserrolesService.saveBatch(list);
        if(res){
            return new Result(0,"批量添加成功");
        }else{
            return new Result(0,"批量添加失败");
        }
    }

    /**
     * 给指定用户批量移除角色
     * @param uid 指定的用户
     * @param rids 角色id组成的字符串
     * @return
     */
    @PostMapping("/delRolesByUserId")
    public Object delRolesByUserId(@RequestParam Integer uid,@RequestParam String rids){
        //调用方法，批量添加集合中的数据
        boolean res = iUserrolesService.delRolesByUserId(uid,rids);
        if(res){
            return new Result(0,"批量移除成功");
        }else{
            return new Result(0,"批量移除失败");
        }
    }


}
